package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.OrderSettingDao;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderService;
import org.aspectj.weaver.ast.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = OrderService.class)
@Transactional
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderSettingDao orderSettingDao;
    @Autowired
    private MemberDao memberDao;
    @Autowired
    private OrderDao orderDao;
    @Override
    public Result submit(Map map)throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = sdf.parse((String) map.get("orderDate"));
        OrderSetting dbOrderSetting = orderSettingDao.findByOrderDate(date);
        // 判断所选日期是否进行了预约设置
        if (dbOrderSetting==null){
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }

        int number = dbOrderSetting.getNumber();
        int reservations = dbOrderSetting.getReservations();
        // 判断所选日期预约是否已满
        if (reservations>=number){
            return new Result(false, MessageConstant.ORDER_FULL);
        }

        // 判断这个人是不是已经存在的会员
        Member member = memberDao.findByTelephone((String) map.get("telephone"));
        if (member==null){
            //自动注册为会员
            member = new Member();
            member.setName((String) map.get("name"));
            member.setSex((String) map.get("sex"));
            member.setPhoneNumber((String) map.get("telephone"));
            member.setIdCard((String) map.get("idCard"));
            member.setRegTime(new Date());
            // todo 通过身份证获取生日
            // 保存会员信息
            memberDao.add(member);
        }else {
            // 会员是否已经有预约订单
            Order order = new Order();
            order.setMemberId(member.getId());
            order.setOrderDate(date);
            List<Order> list = orderDao.findByCondition(order);
            if (list!=null&&list.size()>0){
                return new Result(false,MessageConstant.HAS_ORDERED);
            }
        }
        Order order = new Order();
        order.setMemberId(member.getId());
        order.setOrderDate(date);
        order.setOrderType((String)map.get("OrderType"));
        order.setOrderStatus("未到");
        order.setSetmealId(Integer.parseInt((String) map.get("setmealId")));
        // 新增预约订单
        orderDao.add(order);
        // 设置已预约人数加一
        dbOrderSetting.setReservations(dbOrderSetting.getReservations()+1);
        // 更新已预约信息
        orderSettingDao.editReservationsByOrderDate(dbOrderSetting);
        return new Result(true, MessageConstant.ORDER_SUCCESS,order.getId());
    }

    @Override
    public Map findById(Integer OrderId) {
        Map map = orderDao.findById4Detail(OrderId);
        return map;
    }

    // 我自己写的方法
    public Result mySubmit(Map<String, Object> map)throws Exception {
        Object orderDate = map.get("orderDate");
        Object setmealId = map.get("setmealId");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = sdf.parse((String) orderDate);

        OrderSetting dbOrderSetting = orderSettingDao.findByOrderDate(date);

        // 判断所选日期是否进行了预约设置
        if (dbOrderSetting!=null){
           int available = dbOrderSetting.getNumber()- dbOrderSetting.getReservations();
           // 判断所选日期预约是否已满
           if (available>0){
               // 判断这个人是不是已经存在的会员
               Member byTelephone = memberDao.findByTelephone((String) map.get("telephone"));
               if (byTelephone==null){
                   //自动注册为会员
                   Object name = map.get("name");
                   Object telephone = map.get("telephone");
                   Object idCard = map.get("idCard");
                   Object sex = map.get("sex");
                   Member member = new Member();
                   member.setName((String) name);
                   member.setSex((String) sex);
                   member.setPhoneNumber((String) telephone);
                   member.setIdCard((String) idCard);
                   member.setRegTime(new Date());
                   // todo 通过身份证获取生日
                   // 保存会员信息
                   memberDao.add(member);
                   byTelephone=member;
                   //如果是新会员,就不用查询是否有预约订单,直接添加即可
                   // 设置已预约人数加一
                   dbOrderSetting.setReservations(dbOrderSetting.getReservations()+1);
                   // 更新已预约信息
                   orderSettingDao.editReservationsByOrderDate(dbOrderSetting);
                   Order order = new Order();
                   order.setMemberId(byTelephone.getId());
                   order.setOrderDate(date);
                   order.setOrderType("测试类别");
                   order.setOrderStatus("未到");
                   String setmealIdStr = (String) setmealId;
                   order.setSetmealId(Integer.parseInt(setmealIdStr));
                   // 新增预约订单
                   orderDao.add(order);
                   return new Result(true, MessageConstant.ORDER_SUCCESS,order.getId());
               }else {
               // 会员是否已经有预约订单
                   Order order1 = new Order();
                   order1.setMemberId(byTelephone.getId());
                   order1.setOrderDate(date);
                   List<Order> byCondition = orderDao.findByCondition(order1);
               if (byCondition.size()==0){
                   // 初始化订单信息
                   Order order = new Order();
                   order.setMemberId(byTelephone.getId());
                   order.setOrderDate(date);
                   order.setOrderType("测试类别");
                   order.setOrderStatus("未到");
                   String setmealIdStr = (String) setmealId;
                   order.setSetmealId(Integer.parseInt(setmealIdStr));
                   // 没有重复预约的信息
                   // 设置已预约人数加一
                   dbOrderSetting.setReservations(dbOrderSetting.getReservations()+1);
                   // 更新已预约信息
                   orderSettingDao.editReservationsByOrderDate(dbOrderSetting);
                   // 新增预约订单
                    orderDao.add(order);
                   return new Result(true, MessageConstant.ORDER_SUCCESS,order.getId());
               }else {
                   return new Result(false,MessageConstant.HAS_ORDERED);
               }
               }
           }else {
               return new Result(false, MessageConstant.ORDER_FULL);
           }
        }else {
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }
    }

}
